package com.wwwarehouse.resource_center.fragment.defindeobjectstore;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.RelativeLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.adapter.defindeobjectstore.DefinedObjectStoreAdapter;
import com.wwwarehouse.resource_center.bean.defindeobjectstore.DefinedObjectStoreBean;
import com.wwwarehouse.resource_center.bean.defindeobjectstore.QueryBean;
import com.wwwarehouse.resource_center.constant.ResourceConstant;
import com.wwwarehouse.resource_center.fragment.createres.ResDescripFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Description: 定义实物库存第一个页面
 * Copyright  : Copyright (c) 2017
 * Author     : minifly
 * Date       : 2017/10/25
 * type :  INVENTORY_DEFINITION
 */
@Route(path = "/ResourceObjec/DefinedObjectAllobjectFragment")
public class DefinedObjectAllobjectFragment extends BaseFragment {

    private View v;
    private RecyclerView mDefinedObjectRecycleview;
    private StateLayout mStateLayout;
    private Map<String, Object> mGetInventMap;
    private Bundle mToDetailBundle;
    private DefinedObjectDealFragment mDefinedObjectDealFragment;
    private long mBusinessUnitUkid;
    private int mPage;
    private DefinedSelfDetailFinishedFragment mDefinedSelfDetailFinishedFragment;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_defined_object_list, container, false);
        initView(v);
        initData();
        return v;
    }

    private void initData() {
        if (getArguments() != null) {
            Bundle bundle = getArguments();
            mBusinessUnitUkid = bundle.getLong("businessUnitUkid");
            mPage = bundle.getInt("page");
            mGetInventMap = new HashMap<>();
            mGetInventMap.put("businessUnitUkid", mBusinessUnitUkid);
            QueryBean queryBean = new QueryBean();
            queryBean.setPage(mPage);
            queryBean.setSize(5);
            mGetInventMap.put("query", queryBean);
            getInvent();
        }
        mToDetailBundle = new Bundle();
        mDefinedObjectDealFragment = new DefinedObjectDealFragment();
        mDefinedSelfDetailFinishedFragment = new DefinedSelfDetailFinishedFragment();
    }

    private void getInvent() {
        mStateLayout.showProgressView(true);
        NoHttpUtils.httpPost(ResourceConstant.GET_INVENTORY_LIST, mGetInventMap, mOnResponseListener, 0);
    }

    private NoHttpUtils.OnResponseListener mOnResponseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int i) {
            try {
                if ("0".equals(commonClass.getCode())) {
                    final DefinedObjectStoreBean definedObjectStoreBean = JSON.parseObject(commonClass.getData().toString(), DefinedObjectStoreBean.class);
                    final ArrayList<DefinedObjectStoreBean.ListBean> list = (ArrayList<DefinedObjectStoreBean.ListBean>) definedObjectStoreBean.getList();
                    DefinedObjectStoreAdapter definedObjectStoreAdapter = new DefinedObjectStoreAdapter(list, mActivity);
                    mDefinedObjectRecycleview.setAdapter(definedObjectStoreAdapter);
                    definedObjectStoreAdapter.setOnItemListener(new DefinedObjectStoreAdapter.OnItemListener() {
                        @Override
                        public void onItem(RelativeLayout inRel, int position) {
                            DefinedObjectStoreBean.ListBean listBean = list.get(position);
                            mToDetailBundle.putParcelable("listBean", listBean);
                            mToDetailBundle.putLong("businessUnitUkid", mBusinessUnitUkid);
                            if ("1".equals(listBean.getDefaultInventory())) {
                                mDefinedObjectDealFragment.setArguments(mToDetailBundle);
                                pushFragment(mDefinedObjectDealFragment);
                            } else {
                                mDefinedSelfDetailFinishedFragment.setArguments(mToDetailBundle);
                                pushFragment(mDefinedSelfDetailFinishedFragment);
                            }

                        }
                    });
                }
            } catch (Exception e) {
                LogUtils.showLog(e.toString());
            }
        }

        @Override
        public void onFailed(String s, int i) {
            mStateLayout.showNetworkView(true);//网络问题
            mStateLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getInvent();
                }
            });
        }

        @Override
        public void onFinish(int i) {
            mStateLayout.showContentView();
        }
    };

    private void initView(View v) {
        mStateLayout = (StateLayout) v.findViewById(R.id.sl_layout);
        mDefinedObjectRecycleview = (RecyclerView) v.findViewById(R.id.defined_object_recycleview);
        mDefinedObjectRecycleview.setLayoutManager(new LinearLayoutManager(mActivity));
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof DefinedObjectAllobjectFragment)
            mActivity.setTitle("" + getString(R.string.defined_object_store_title));
    }

    @Override
    public void requestDatas() {

    }

}
